package com.health.mapp.impl;

import com.health.db.model.UserCashAccountIdNo;
import com.health.db.model.UserCashAccountIdNoExample;
import com.health.db.model.UserCashAccountIdNoExample.Criteria;
import com.health.db.model.UserLIST;
import com.health.db.model.WXUserInfo;
import com.health.db.service.sm.IWXService;
import com.health.db.service.user.IUserCashAccountIdNoService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.*;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import org.apache.http.util.TextUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/31.
 */
public class AddAccountImpl implements ActionInterface {

    private IUserCashAccountIdNoService userCashAccountIdNoService;
    private IWXService wxService;
    private IUserLISTService userLISTService;

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        AddAccountReq req = (AddAccountReq) reqBean;
        AddAccountResp _resp = new AddAccountResp();

        if (null==req.getUserId() || null==req.getAccountType() || TextUtils.isEmpty(req.getCode())) {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
            return _resp;
        }
        UserCashAccountIdNoExample example=new UserCashAccountIdNoExample();
        Criteria criteria=example.createCriteria();
        criteria.andOwneridEqualTo(req.getUserId());
        criteria.andAccounttypeidEqualTo(req.getAccountType());
        criteria.andIsUsedFlagEqualTo((short) 1);
        List<UserCashAccountIdNo> userCashAccountIdNos=userCashAccountIdNoService.queryUserCashAccountIdNo(example);
        if (null!=userCashAccountIdNos&&userCashAccountIdNos.size()>0)
        {
            /**
             * 存在此账户类型 不能再添加
             * */
            _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5000);
            _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5000_NOTE);
            return _resp;
        }else
        {
            UserLIST user=userLISTService.selectByUserId(req.getUserId());
            if (null==user)
            {
                /**
                 * 用户ID有问题用户不存在
                 * */
                _resp.setErrorCode(ResultCode.LOGIN_FAIL_NAME_ERROR);
                _resp.setErrorMsg(ResultCode.LOGIN_FAIL_NAME_ERROR_NOTE);
                return _resp;
            }else
            {
                if (1==req.getAccountType())
                {
                    //添加微信账户
                    WXTokenModel tokenModel=wxService.getAccessToken(req.getCode());
                    if (null!=tokenModel&&!TextUtils.isEmpty(tokenModel.getOpenid()))
                    {
                        //通过授权码获取用户信息成功
                        WXUserInfo wxUserInfo=wxService.getWXUserInfo(tokenModel.getAccess_token(),tokenModel.getOpenid());
                        if (null!=wxUserInfo)
                        {
                            Date now=new Date();
                            UserCashAccountIdNo userCashAccountIdNo=new UserCashAccountIdNo();
                            userCashAccountIdNo.setSystemid(0);
                            userCashAccountIdNo.setOwnerid(req.getUserId());
                            userCashAccountIdNo.setAccountBankName("微信");
                            userCashAccountIdNo.setAccountIdno("weixin");
                            userCashAccountIdNo.setAccountName(wxUserInfo.getNickname());
                            userCashAccountIdNo.setAccountIdentityid(tokenModel.getOpenid());
                            userCashAccountIdNo.setAccounttypeid(1);
                            userCashAccountIdNo.setAccountTypeName("微信");
                            userCashAccountIdNo.setContactPhone(user.getUserNo());
                            userCashAccountIdNo.setIsUsedFlag((short) 1);
                            userCashAccountIdNo.setCreateDate(now);
                            userCashAccountIdNo.setUpdateDate(now);
                            userCashAccountIdNo.setCreateUserid(req.getUserId());
                            userCashAccountIdNo.setUpdateUserid(req.getUserId());

                            userCashAccountIdNoService.insertUserCashAccountIdNo(userCashAccountIdNo);

                            _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
                            _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);

                            return _resp;

                        }else
                        {
                            _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5002);
                            _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5002_NOTE);
                            return _resp;
                        }

                    }else
                    {
                        _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5001);
                        _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5001_NOTE);
                        return _resp;
                    }

                }else
                {
                    _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5001);
                    _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5001_NOTE);
                    return _resp;
                }
            }

        }
    }

    public IUserCashAccountIdNoService getUserCashAccountIdNoService() {
        return userCashAccountIdNoService;
    }

    public void setUserCashAccountIdNoService(IUserCashAccountIdNoService userCashAccountIdNoService) {
        this.userCashAccountIdNoService = userCashAccountIdNoService;
    }

    public IWXService getWxService() {
        return wxService;
    }

    public void setWxService(IWXService wxService) {
        this.wxService = wxService;
    }

    public IUserLISTService getUserLISTService() {
        return userLISTService;
    }

    public void setUserLISTService(IUserLISTService userLISTService) {
        this.userLISTService = userLISTService;
    }
}
